/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var FormModelObject = {
    'string': {
        'name':'string',
        'label':'string',
        'type':'button|checkbox|color|date|datetime|datetime-local|email|file|hidden|image|month|number|password|radio|range|reset|search|submit|tel|text|time|url|week(default:text)',
        'value':['integer','string','object'],
        'default':'string',
        'required':'boolean',
        'readonly':'boolean',
        'disabled':'boolean',
        'typetag':'textarea|input|select|checkbox|radio(default:input)'
    }
};
var Interpreter = $.inherit({
    templates :{
        textarea:'<textarea id="{name}" name="{name}" class="">{value}</textarea>',
        input:'<input {selected} {checked} type="{type}" id="{name}" name="{name}" value="{value}" class="" />',
        labelInput:'<label class="{type} inline"><input {selected} {checked} type="{type}" id="{name}" name="{name}" value="{value}" class="" />{label}</label>',
        option:'<option value="{value}">{label}</option>',
        select:'<select id="{name}" name="{name}">{options}</select>',
        line:'<div class="control-group">   <label for="{name}" class="control-label">{label}</label>  <div class="controls">{content}</div>    </div>' 
    },
    datas:{},
    __constructor:function(datas){
        this.datas = datas;
    },
    parse:function(){
        
        var content = '';
        var options = '';
        var subtemplate = '';
        
        var template = this.templates;
        var datas = this.datas;
        
        if(datas.multiple){
            datas.multiple = 'multiple="multiple"';
        }
        
        //Gestion spécifique des boolean
        if(datas.type == 'boolean'){
            datas['default'] = datas.value;
            datas.value = {'Vrai':'Vrai','Faux':'Faux'};
            datas.type = 'radio' ;
        }
        
        var typetag = datas.typetag ? datas.typetag :'input' ;
        
        if(datas.value && typeof datas.value == 'object'){ //select|checkbox|radio
            
            if(typetag == 'select'){//alors on prend les options
                
                $.each(datas.value,function(key,value){
                    
                    var label = '';
                    
                    if(typeof key == 'string'){
                        attr.label = key;
                    }
                    
                    var attr = {
                        'label':label,
                        'value':value
                    };
                    
                    if(datas['default'] == value){
                        attr.selected = 'selected="selected"';
                    }
                    
                    options += $.nano(template.option, attr);
                });
                
                subtemplate = $.nano(template.select, datas);
                content = $.nano(subtemplate, {
                    'options':options
                });
                
            }else{
                
                $.each(datas.value,function(key,value){
                    
                    var attr = $.extend(datas,{});
                    
                    attr.label = '';
                    attr.value = value;
                    
                    if(typeof key == 'string'){
                        console.warn(key,' ', template.labelInput);
                        attr.label = key;
                    }
                    console.info(datas['default'], value, datas.type);
                    
                    if(datas['default'] == value){
                        console.warn('ok ok');
                        attr.selected = 'checked="checked"';
                    }
                    
                    content += $.nano(template.labelInput, attr);
                });
                
            }
             
        }else{//textarea|input (simple)
            
            subtemplate = $.nano(template.input, datas);
            content = $.nano(subtemplate, datas);
        }
        
        var s = $.nano(template.line, {
            'content':content
        });
        
//        console.log(content);
//        console.warn(datas);
//        console.info(s);
        
        return s;

    }
});